package com.gebogebo.android.justads;

import static java.lang.String.format;

import java.util.HashMap;
import java.util.Map;

import com.gebogebo.android.justads.UserPreferences.Gender;
import com.google.ads.AdRequest;
import com.google.ads.AdSize;
import com.google.ads.AdView;

import android.app.Activity;
import android.util.Log;
import android.view.Gravity;
import android.widget.LinearLayout;

public class AdMobManager implements AdManager {
    private static final String lg = "admobManager";
    private static final String AD_MOB_KEY = "<TODO: add key here>";//ja

    private com.google.ads.AdRequest.Gender gender = null;
    private final Map<String, Object> extrasMap = new HashMap<String, Object>();
    private final AdMobAdListener listener;
    
    public AdMobManager(UserPreferences prefs, AdMobAdListener listener) {
        if(prefs != null) {
            if(prefs.getUserGender() != null) {
                if(Gender.MALE.equals(prefs.getUserGender())) {
                    this.gender = com.google.ads.AdRequest.Gender.MALE;
                    Log.i(lg, "setting gender as male");
                } else {
                    this.gender = com.google.ads.AdRequest.Gender.FEMALE;
                    Log.i(lg, "setting gender as female");
                }
            }            
            if(prefs.getUserMaritalStatus() != null) {
                extrasMap.put(UserPreferences.MARITAL_KEY, prefs.getUserMaritalStatus().toString());
            }
            if(prefs.getAge() > 0) {
                extrasMap.put(UserPreferences.AGE_KEY, prefs.getAge());
            }
        }
        this.listener = listener;
    }

    public void requestAd(Activity activity, LinearLayout ll) {
        Log.i(lg, "adding admob ads");
        addAdMobAd(activity, ll, AdSize.BANNER, 0);// 0 = no refresh
        addAdMobAd(activity, ll, AdSize.IAB_MRECT, 0);
        addAdMobAd(activity, ll, AdSize.BANNER, 0);
    }
    
    public int getAdsPerRequest() {
        return 3;
    }
    
    /**
     * adds admob ad to given layout with given parameters
     * (refresh time isn't working as of now)
     * 
     * @param ll layout in which admob ad is to be added
     * @param size admob adSize
     * @param refreshSecs ad refresh time in secs
     */
    private void addAdMobAd(Activity activity, LinearLayout ll, AdSize size, long refreshSecs) {
        Log.i(lg, format("adding admob ad of size %s", size));
        AdView adView = new AdView(activity, size, AD_MOB_KEY);
        adView.setAdListener(listener);
        adView.setPadding(0, ADS_PADDING, 0, ADS_PADDING);
        ll.addView(adView);
        ll.setGravity(Gravity.CENTER_HORIZONTAL);
        AdRequest adRequest = new AdRequest();
        
        populateWithPreferences(adRequest);
        // adRequest.addTestDevice("73B900443E12DCC5C4AB19581FDE6AC6");
        adView.loadAd(adRequest);
    }
    
    private void populateWithPreferences(AdRequest req) {
        Log.i(lg, format("setting gender %s and extras %s in admob request", gender, extrasMap));
        req.setGender(gender);
        req.setExtras(extrasMap);
    }
}
